<?php

namespace App\Store\Controllers\OldMaterial;

use App\Common\Services\CommonExcelExport;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\OldMaterial\OldData;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\OldMaterial\OldData as ValidateOldMaterialOldData;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 旧料存入
 */
class OldDataController extends Controller
{
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_name', 'order_sncode', 'stockin_at', 'stock_type', 'goods_sn_code', 'goods_category', 'name', 'count', 'piece_weight', 'brand', 'goods_pricetype', 'main_stone_weigth', 'main_stone_count', 'other_stone_weigth', 'other_stone_count', 'color', 'clarity', 'exchange_amount', 'cf_no']);
        $Validate = new ValidateOldMaterialOldData();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $req_data['piece_weight'] = !empty($req_data['piece_weight']) ? $req_data['piece_weight'] : 0;
        $req_data['main_stone_weigth'] = !empty($req_data['main_stone_weigth']) ? $req_data['main_stone_weigth'] : 0;
        $req_data['main_stone_count'] = !empty($req_data['main_stone_count']) ? $req_data['main_stone_count'] : 0;
        $req_data['other_stone_weigth'] = !empty($req_data['other_stone_weigth']) ? $req_data['other_stone_weigth'] : 0;
        $req_data['other_stone_count'] = !empty($req_data['other_stone_count']) ? $req_data['other_stone_count'] : 0;
        $req_data['exchange_amount'] = !empty($req_data['exchange_amount']) ? $req_data['exchange_amount'] : 0;
        $old_data = $req_data + [
            'cp_id' => $cp_id
        ];
        OldData::create($old_data);
        return response()->json(['code' => '000', 'msg' => '添加成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $request->merge(['cp_id' => $cp_id]);
        $list = OldData::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }
    /**
     *  批量删除列表数据
     *  $params []
     *
     * */
    public function dodel(Request $request)
    {
        $ids = $request->id;
        //dd($ids);
        if(!isset($ids)) return response()->json(['code' => '001', 'msg' => 'id不能为空']);
        if(!is_array($ids))
        {
            $ids = [$ids];
        }
        $row = OldData::whereIn('id',$ids)->update(['is_del' => 2]);
        //dd($row);
        if($row > 0){
            return response()->json(['code' => '000', 'msg' => '删除成功', 'data' => $row]);
        }else{
            return response()->json(['code' => '001', 'msg' => '删除失败', 'data' => $row]);
        }
    }


    /**
     * 导入
     */
    public function doimport(Request $request)
    {
        $filepathname = $request->filepathname;
        if ($filepathname != '' && file_exists(storage_path() . $filepathname) && is_file(storage_path() . $filepathname)) {
            $file = storage_path() . $filepathname;
            $data = Excel::toArray(null, $file)[0];

            try {
                foreach ($data as $key => $val) {
                    //表头跳过
                    if ($key == 0) {
                        continue;
                    }
                    $import['store_name'] = $val[0] ?? '';
                    $import['order_sncode'] = $val[1] ?? '';
                    $import['stockin_at'] = strtotime($val[2]) ?? time();
                    $import['stock_type'] = $val[3] ?? '';
                    $import['goods_sn_code'] = $val[4] ?? '';
                    $import['goods_category'] = $val[5] ?? '';
                    $import['name'] = $val[6] ?? '';
                    $import['count'] = $val[7] ?? 0;
                    $import['piece_weight'] = $val[8] ?? 0;
                    $import['brand'] = $val[9] ?? '';
                    $import['goods_pricetype'] = $val[10] ?? '';
                    $import['cf_no'] = $val[11] ?? '';
                    $import['main_stone_weigth'] = $val[12] ?? 0;
                    $import['main_stone_count'] = $val[13] ?? 0;
                    $import['other_stone_weigth'] = $val[14] ?? 0;
                    $import['other_stone_count'] = $val[15] ?? 0;
                    $import['color'] = $val[16] ?? '';
                    $import['clarity'] = $val[17] ?? '';
                    $import['exchange_amount'] = $val[18] ?? 0;
                    OldData::insert($import);
                }
                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
                return response()->json(['code' => '001', 'msg' => '添加失败' . $e->getMessage()]);
            }
            return response()->json(['code' => '000', 'msg' => '添加成功']);
        }
        return response()->json(['code' => '001', 'msg' => '文件不存在']);
    }

    /**
     * 模板下载
     */
    public function dotemplate()
    {
        $exceldata = [];
        $row = [[
            'store_name' => '回收门店',
            'order_sncode' => '订单号',
            'stockin_at' => '回收时间',
            'stock_type' => '回收方式',
            'goods_sn_code' => '条码',
            'goods_category' => '旧料类别',
            'name' => '旧料名称',
            'count' => '数量',
            'piece_weight' => '重量',
            'brand' => '品牌',
            'goods_pricetype' => '价格类型',
            'cf_no' => '证书',
            'main_stone_weigth' => '主石重',
            'main_stone_count' => '主石数',
            'other_stone_weigth' => '副石重',
            'other_stone_count' => '副石数',
            'color' => '颜色',
            'clarity' => '净度',
            'exchange_amount' => '换货金额',
        ]];

        $filename = '存入旧料导入模板_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata,[],[],false), $filename);
    }
}
